﻿var CalculatorMenu = function (nationalItem, cityItem, restaurantItem, restaurantTypeItem,
            menuElement, menuTypeElement, deginDate, priceAdultsMenu, priceChildsMenu,
            quantityAdultsMenu, quantityChildMenu, vatMenu, totalPriceMenu, noteElement,
            seasonTour, tourInfo, quantityBaby, riceBaby, userIdElement) {
 
    //$(priceAdultsMenu).val(0);
    //$(priceChildsMenu).val(0);
    //$(quantityAdultsMenu).val(0);
    //$(quantityChildMenu).val(0);
    //$(vatMenu).val(0);
    
    var loadCity_Nationalid = function ( cityItemId,  RestaurantId, menuElementId,cityName,resName) {
        var nationalId = $(nationalItem).val();
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "ListCityByNational", "NationalId": nationalId },
            dataType: "json",
            success: function (data) {
                $(cityItem).html('<option value="-1">--- Chọn tỉnh/thành phố ---</option>');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $(cityItem)
                            .append($("<option></option>")
                                .attr("value", item.ID)
                                .text(item.Name));
                    });
                }

                $(cityItem).combobox('setValue', cityName);
                $(cityItem).val(cityItemId);
                loadRestaurant_RestaurantTypeId(RestaurantId, menuElementId, resName);
            }
        });

    };
    $(nationalItem).combobox({
        select: function (event, ui) {
            loadCity_Nationalid();
        }
    });
    var loadRestaurant_RestaurantTypeId = function (RestaurantId, menuElementId, resName) {

        var restaurantTypeId = $(restaurantTypeItem).val();
        var cityId = $(cityItem).val();
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "ListRestaurantByRestaurantType", "RestaurantTypeId": restaurantTypeId, "CityId": cityId },
            dataType: "json",
            success: function (data) {
                $(restaurantItem).html('<option value="-1">--- Chọn nhà hàng---</option>');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $(restaurantItem)
                            .append($("<option></option>")
                                .attr("value", item.ID)
                                .text(item.Name));
                    });
                }

                $(restaurantItem).combobox('setValue', resName);
                $(restaurantItem).val(RestaurantId);
                loadByMenu( menuElementId);
            }
        });

    };
    $(restaurantTypeItem).change(loadRestaurant_RestaurantTypeId);
    $(cityItem).combobox({
        select: function (event, ui) {
            loadRestaurant_RestaurantTypeId();
        }
    });
    //load thực đơn
    var loadByMenu = function (menuElementId) {

        var typeId = 4;
        var restaurantId = $(restaurantItem).val();
        var menuTypeElementid = $(menuTypeElement).val();
        var date = $(deginDate).val();
        var season = seasonTour;
        if (restaurantId <= 0 || menuTypeElementid <= 0 || date == NaN) {
            $(menuElement).html('<option value="-1">--- Chọn thực đơn ---</option>'); return;
        }
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "PriceListByMenu", "TypeId": typeId, "RestaurantId": restaurantId, "MenuTypeId": menuTypeElementid, "SeasonTour": season, "Date": date },
            dataType: "json",
            success: function (data) {
                $(menuElement).html('<option value="-1">--- Chọn thực đơn ---</option>');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $(menuElement)
                            .append($("<option></option>")
                                .attr("value", item.PriceId)
                                .text(item.PriceMenu));
                    });
                }
                $(menuElement).val(menuElementId);
            }
        });
    };
    
   
    // goi để load thực đơn
    $(restaurantItem).combobox({
        select: function (event, ui) {
            loadByMenu();
        }
    });
    $(deginDate).change(loadByMenu);
    $(menuTypeElement).change(loadByMenu);
    //load thực đơn
    var loadPricel = function () {
        var menuElementId = $(menuElement).val();
       
        $.ajax({
            type: "POST",
            url: "/ajax_hc.html",
            data: { "function": "PriceTicKetAri", "TicketId": menuElementId },
            dataType: "json",
            success: function (data) {
                if (data && data.isError == false) {
                    $(priceAdultsMenu).val(data.PriceAdult);
                    $(priceChildsMenu).val(data.PriceChild);
                    $(riceBaby).val(data.PriceBaby);
                    funTong();
                }
            }

        });
    };
    var funTong = function () {
        var priceAdults = $(priceAdultsMenu).val();
        var priceChilds = $(priceChildsMenu).val();
        var quantityAdults = $(quantityAdultsMenu).val();
        var quantityChild = $(quantityChildMenu).val();
        
        var quantityBabys = $(quantityBaby).val();
        var riceBabys = $(riceBaby).val();
        var vat = $(vatMenu).val();
        var total = 0;
        
       total = ((parseInt(priceAdults) * parseInt(quantityAdults)) + (parseInt(priceChilds) * parseInt(quantityChild)+  (parseInt(riceBabys) * parseInt(quantityBabys))));
            
        
       $(totalPriceMenu).val(total);
       $(vatMenu).val(total*0.1);
    };
    // goi load gia
    $(menuElement).change(loadPricel);
    $(quantityAdultsMenu).keyup(funTong);
    $(quantityChildMenu).keyup(funTong);
    $(priceAdultsMenu).keyup(funTong);
    $(priceChildsMenu).keyup(funTong);
    $(quantityBaby).keyup(funTong);
    $(riceBaby).keyup(funTong);
   
    
    //ham them sua xoa
    var fncRestaurant = function() {

        var typeId = 4;
        var tourId = parseInt(tourInfo);
        var dateMenu = $(deginDate).val();
        var menuElementId = $(menuElement).val();
        var priceAdults = $(priceAdultsMenu).val();
        var priceChilds = $(priceChildsMenu).val();
        var quantityAdults = $(quantityAdultsMenu).val();
        var quantityChild = $(quantityChildMenu).val();

        var quantitybaby = $(quantityBaby).val();
        var priceBaby = $(riceBaby).val();
        var vat = $(vatMenu).val();
        if (vat > 0 ) {
        } else {
            vat = 0;
        }
        var note = $(noteElement).val();
        var calculatorId = $(this).attr("calculatorid");
        var className = $(this).attr("class");

        var concern = $(restaurantItem).val();
        var userId = parseInt(userIdElement);

        $.ajax({
            type: "POST",
            url: "/ajax_hc.html",
            data: {
                "function": "InsertMenu",
                "TypeId": typeId,
                "TourId": tourId,
                "menuElementID": menuElementId,
                "PriceAdults": priceAdults,
                "PriceChilds": priceChilds,
                "Date": dateMenu,
                "CalculatorId": calculatorId,
                "ClassName": className,
                'Vat': vat,
                'QuantityAdults': quantityAdults,
                'QuantityChild': quantityChild,
                "QuantityBaby": quantitybaby,
                "PriceBaby": priceBaby,
                'Note': note, 'Concern': concern, 'UserId': userId
            },
            dataType: "json",
            success: function(data) {

                $(".ListMenu").remove();

                $(nationalItem).val('-1');
                $(cityItem).val('-1');
                $(restaurantItem).val('-1');
                $(restaurantTypeItem).val('-1');
                $(menuElement).val('-1');
                $(menuTypeElement).val('-1');
                $(deginDate).val('');
                $(priceAdultsMenu).val('0');
                $(priceChildsMenu).val('0');
                $(quantityAdultsMenu).val('0');
                $(quantityChildMenu).val('0');
                $(quantityAdultsMenu).val('0');


                $(quantityBaby).val('0');
                $(riceBaby).val('0');

                $(vatMenu).val('0');
                $(totalPriceMenu).val('0');
                $(noteElement).val('0');
                if (data && data.isError == false) {
                    $.each(data.array, function(index, item) {
                        
                        $('#new-Menu')
                            .before($("<tr class='ListMenu' id='menu-" + item.CalculatorId + "'></tr>").html('<td></td>' +
                                
                                '<td >' + item.RestaurantFullName + '</td>' +
                                '<td >' + item.DateBegin + '</td>' +
                                '<td >' + item.MenuTypeName + '</td>' +
                                '<td >' + item.MenuName + '</td>' +
                                '<td >' + item.PriceAdults + '</td>' +
                                '<td >' + item.PriceChild + '</td>' +
                                '<td >' + item.PriceBaby + '</td>' +
                                '<td >' + item.QuantityAdults + '</td>' +
                                '<td >' + item.QuantityChild + '</td>' +
                                '<td >' + item.QuantityBaby + '</td>' +
                                
                                '<td >' + item.TotalPriceAir + '</td>' +
                                '<td >' + item.Vat + '</td>' +
                                '<td></td>' +
                                '<td>' +
                                '<input class="btnUpdateMenu" onclick="openPopup(\'ticket-restaurant\')"  type="button" value="" calculatorid="' + item.CalculatorId + '"   />' +
                                '<input class="btnDeleteMenu"  type="button" value="" calculatorid="' + item.CalculatorId + '"   />' +
                                '</td>'));
                    });
                    $('#TotalMenuPrices').html(data.TotalMenuPrices);
                    $('#TotalMenuAdults').html(data.TotalMenuAdults);
                    $('#TotalMenuChild').html(data.TotalMenuChild);
                    $('#TotalMenuBaby').html(data.TotalPriceBaby);
                    $('#TotalVatRestaurant').html(data.TotalVatRestaurant);
                    $('#btnInsertMenu').attr("calculatorid", "-1").val("Thêm");
                    TotalPrice();
                    fncTotalCalculator();
                    hidePopup('ticket-restaurant');
                }

            }
        });
        
    };

    $('#btnInsertMenu').on("click", fncRestaurant);
    $('#btnDeleteMenu').on("click", fncRestaurant);
    $('table').on("click", 'tr >td >input.btnDeleteMenu', fncRestaurant);
    $('table').on("click", 'tr >td >input.btnUpdateMenu', function () {
        var calculatorId = $(this).attr("calculatorid"); 
        var callAjax = $.ajax({
            type: "POST",
            url: "/ajax_hc.html",
            data: { "function": "UpdateMenu", "CalculatorId": calculatorId },
            dataType: "json",
            success: function (data) {
           
                $('#menu-' + calculatorId).remove();

                $(nationalItem).combobox('setValue', data.NationalName);
                $(nationalItem).val(data.Nationalid);
                loadCity_Nationalid(data.CityId, data.RestaurantId, data.MenuId, data.CityName, data.RestaurantIName);
            
                $(restaurantTypeItem).val(data.RestaurantTypeId);
                //$(menuElement).val(data.MenuId);
                $(menuTypeElement).val(data.MenuTypeId);
                $(deginDate).val(data.DateBegin);
                $(priceAdultsMenu).val(data.PriceAdults);
                $(priceChildsMenu).val(data.PriceChild); 
                $(riceBaby).val(data.PriceBaby);
                $(quantityAdultsMenu).val(data.QuantityAdults);
                $(quantityChildMenu).val( data.QuantityChild); 
                $(quantityBaby).val(data.QuantityBaby); 
                
                $(vatMenu).val(data.Vat);
                $(totalPriceMenu).val(data.TotalPriceAir);
                $(noteElement).val(data.Note);
                $('#btnInsertMenu').attr("calculatorid", calculatorId).val("Lưu");
                
            }
        });
    });
};

